package timethingy.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;


public class MainFrame extends JFrame {
	
	public MainFrame() {
		init();
		
		createContents();
		
		pack();
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	private JTextField fieldActivity;
	private JTextField fieldLabels;
	private JFormattedTextField fieldDate;
	private JTextField fieldStartHour;
	private JTextField fieldStartMinute;
	private JTextField fieldEndHour;
	private JTextField fieldEndMinute;
	
	private CellConstraints cc = new CellConstraints();
	
	private void createContents() {
		FormLayout layout = new FormLayout("5dlu, pref, 8dlu, pref:grow, 5dlu","10dlu, pref, 5dlu, pref, 5dlu, pref, 15dlu, pref, 5dlu, pref, 5dlu, pref, 5dlu, pref, 5dlu");
		
		
		PanelBuilder builder = new PanelBuilder(layout);
		builder.setBackground(Color.WHITE);
		
		builder.addSeparator("Activity", cc.xyw(2, 2, 3));
		builder.add(new JLabel("Description"), cc.xy(2, 4));
		fieldActivity = new JTextField();
		builder.add(fieldActivity, cc.xy(4, 4));
		builder.add(new JLabel("Labels"), cc.xy(2, 6));
		fieldLabels = new JTextField();
		builder.add(fieldLabels, cc.xy(4, 6));
		
		builder.addSeparator("Date & Time", cc.xyw(2, 8, 3));
		
		JPanel datePanel = createDatePanel();
		builder.add(new JLabel("Date"), cc.xy(2, 10));
		builder.add(datePanel, cc.xy(4, 10));
		
		JPanel startPanel = createStartPanel();
		builder.add(new JLabel("Start"), cc.xy(2, 12));
		builder.add(startPanel, cc.xy(4, 12));
		
		JPanel endPanel = createEndPanel();
		builder.add(new JLabel("End"), cc.xy(2, 14));
		builder.add(endPanel, cc.xy(4, 14));
		
		add(builder.getPanel());
	}

	private JButton startButton;
	private JButton endButton;

	private JPanel createEndPanel() {
		FormLayout layout = new FormLayout("5dlu, pref,30dlu, pref, pref, pref, 3dlu","pref");
		
		PanelBuilder builder = new PanelBuilder(layout);
		builder.setBackground(Color.WHITE);
		
		endButton = new JButton("End");
		endButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.out.println("END ACTIVITY");
			}
		});
		builder.add(endButton, cc.xy(2, 1));
		
		fieldEndHour = new JTextField();
		fieldEndHour.setText("HH");
		
		fieldEndMinute = new JTextField();
		fieldEndMinute.setText("MM");
		
		builder.add(fieldEndHour, cc.xy(4, 1));
		builder.add(new JLabel(":"), cc.xy(5, 1));
		builder.add(fieldEndMinute, cc.xy(6, 1));
		
		return builder.getPanel();
	}

	private JPanel createStartPanel() {
		FormLayout layout = new FormLayout("5dlu, pref,30dlu, pref, pref, pref, 3dlu","pref");
		
		PanelBuilder builder = new PanelBuilder(layout);
		builder.setBackground(Color.WHITE);
		
		startButton = new JButton("Start");
		startButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.out.println("START ACTIVITY");
			}
		});
		builder.add(startButton, cc.xy(2, 1));
		
		fieldStartHour = new JTextField();
		fieldStartHour.setText("HH");
		
		fieldStartMinute = new JTextField();
		fieldStartMinute.setText("MM");
		
		builder.add(fieldStartHour, cc.xy(4, 1));
		builder.add(new JLabel(":"), cc.xy(5, 1));
		builder.add(fieldStartMinute, cc.xy(6, 1));
		
		return builder.getPanel();
	}


	private JPanel createDatePanel() {
		FormLayout layout = new FormLayout("5dlu, pref,3dlu","pref");
		
		PanelBuilder builder = new PanelBuilder(layout);
		builder.setBackground(Color.WHITE);
		
		fieldDate = new JFormattedTextField(new SimpleDateFormat("dd/MM/yyyy"));
		fieldDate.setValue(new Date());
		builder.add(fieldDate, cc.xy(2, 1));
		
		return builder.getPanel();
	}


	private void init() {
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		int width = 500;
		int height = 300;
		if(width > dim.width) width = dim.width;
		if(height > dim.height) height = dim.height;
		
		setPreferredSize(new Dimension(width, height));
		setLocation(dim.width/2-width/2, dim.height/2-height/2);
		setTitle("Time Thingy");
	}
	
}
